import {
    ajaxPost,
    showToast
} from '../../../utils/util';
Page({
    /**
     * 页面的初始数据
     */
    data: {
        page: 0,
        limit: 10,
        address: ['上海市', '上海市', '全部'],
        customItem: '全部',
        footerSource: [{
                title: '选选',
                url: '',
                img: '/images/icon_footer_icon1.png',
                imgSet: '/images/icon_footer_icon1_set.png',
                select: true
            },
            {
                title: '招聘会',
                url: '/pages/student/home/home',
                img: '/images/icon_footer_icon2.png',
                imgSet: '/images/icon_footer_icon2_set.png',
                select: false
            },
            {
                title: '我的',
                url: '/pages/student/mine/mine',
                img: '/images/icon_footer_icon3.png',
                imgSet: '/images/icon_footer_icon3_set.png',
                select: false
            },
        ],
        profess: true,
        tabSource: [],
        tabVal: 0,
        listSource: [],
        workTypeArr: ['全部', '全职', '实习'],
        workTypeVal: 0,
        hotArr: ['最新', '推荐'],
        hotVal: 0,

        majorIndex: [0, 0],
        majorVal: ['全部', '全部'],

        hiddenupdate: true,

    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getTab()
        this.getMajor()
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {},
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        wx.showLoading({
            title: '加载中',
        })
        this.getList()
    },
    changeTab(e) {
        console.log('this.data.tabVal', this.data.tabVal);
        console.log(' e.currentTarget.dataset.index', e.currentTarget.dataset.index);
        if (this.data.tabVal != e.currentTarget.dataset.index) {
            this.setData({
                tabVal: e.currentTarget.dataset.index,
                listSource: [],
                page: 0,
                noGet: false
            })
            wx.showLoading({
                title: '加载中',
            })
            this.getList();
        }

    },
    /**
     * 获取标题栏数据
     */
    getTab() {
        let data = {
            token: wx.getStorageSync('token'),
        }
        ajaxPost('/authuser/jobIntentionList', data, res => {
            if (res.code == 0) {
                this.setData({
                    tabSource: res.data.list
                })
                this.getList()
            }
        })
    },
    /**
     * 获取专业列表
     */
    getMajor() {
        let data = {
            token: wx.getStorageSync("token")
        };
        ajaxPost("/company/positionConfig", data, res => {
            let {
                position_major,
            } = res.data.config_list;
            if (res.code == 0) {
                /**
                 * 专业需求
                 */
                let newMajorOneArr = [];
                for (let i in position_major) {
                    newMajorOneArr.push(position_major[i].name);
                }
                let newMajorTwoArr = [];
                let MajorTwoArr = position_major[0].data;
                for (let i in MajorTwoArr) {
                    newMajorTwoArr.push(MajorTwoArr[i].name);
                }
                let newMajorArr = [newMajorOneArr, newMajorTwoArr];
                this.setData({
                    majorArr: newMajorArr, // 专业需求展示列表
                    majorArrAll: position_major, // 专业需求总列表
                });
            }
        })
    },
    /**
     *  专业需求二级分类选择监听事件
     * @param {*} 穿参
     */
    mahirChange(e) {
        console.log(e);
        if (e.detail.column == 0) {
            let majorArrAll = this.data.majorArrAll;
            let arrTwo = majorArrAll[e.detail.value].data;
            let newTwoArr = [];
            for (let i in arrTwo) {
                newTwoArr.push(arrTwo[i].name);
            }
            let Arr = [this.data.majorArr[0], newTwoArr];
            let newMajorIndex = [e.detail.value, 0];
            this.setData({
                majorArr: Arr,
                majorIndex: newMajorIndex
            });
        }
    },
    /**
     * 专业需求确认
     */
    changeMajor(e) {
        let index = e.detail.value
        let arr = []
        arr.push(this.data.majorArr[0][index[0]])
        arr.push(this.data.majorArr[1][index[1]])
        this.setData({})
        this.setData({
            majorVal: arr,
            listSource: [],
            page: 0,
            noGet: false
        })
        wx.showLoading({
            title: '加载中',
        })
        this.getList();
    },
    /**
     * 获取列表数据
     */
    getList() {
        let data = {
            page: ++this.data.page,
            limit: this.data.limit,
            token: wx.getStorageSync('token'),
            type: Number(this.data.hotVal) + 1,
            nature: this.data.workTypeArr[this.data.workTypeVal],
            province: this.data.address[0],
            city: this.data.address[1],
            district: this.data.address[2],
            major: this.data.majorVal,
        }
        if (this.data.tabSource.length == 0) {
            data.intention_id = 0
        } else {
            data.intention_id = this.data.tabSource[this.data.tabVal].id
        }
        ajaxPost('/authuser/xxPositionList', data, res => {
            if (res.code == 0) {
                let data = res.data.list;
                if (data.length == 0) {
                    this.setData({
                        noGet: true
                    })
                } else {
                    let newarr = this.data.listSource.concat(data)
                    this.setData({
                        listSource: newarr,
                    })

                }
                wx.hideLoading()
            } else {
                showToast('text', res.msg)
            }
        })
    },
    /**
     * 热度筛选
     */
    changeHot(e) {
        if (e.detail.value != this.data.hotVal) {
            this.setData({
                hotVal: e.detail.value,
                listSource: [],
                page: 0,
                noGet: false
            })
            wx.showLoading({
                title: '加载中',
            })
            this.getList();
        }

    },
    /**
     * 全职和兼职切换
     */
    changeWorkType(e) {
        if (e.detail.value != this.data.workTypeVal) {
            this.setData({
                workTypeVal: e.detail.value,
                listSource: [],
                page: 0,
                noGet: false
            })
            wx.showLoading({
                title: '加载中',
            })
            this.getList();
        }

    },
    /**
     * 地址选择
     */
    changeAddress(e) {
        this.setData({
            address: e.detail.value,
            listSource: [],
            page: 0,
            noGet: false
        })
        wx.showLoading({
            title: '加载中',
        })
        this.getList();
    }
})